package client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import server.SaleComInterface;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class SaleRes_GUI extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	/*
	public static void main(String[] args) {
		try {
			SaleRes_GUI dialog = new SaleRes_GUI();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SaleRes_GUI() {
		setBounds(100, 100, 450, 300);
		setModalityType(ModalityType.APPLICATION_MODAL);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel label = new JLabel("\u8BF7\u8F93\u5165\u6295\u8D44\u91D1\u989D\uFF1A");
			label.setBounds(87, 97, 101, 15);
			contentPanel.add(label);
		}
		
		
			
		
		textField = new JTextField();
		textField.setToolTipText("\u6295\u8D44\u91D1\u989D");
		textField.setBounds(221, 94, 66, 21);
		contentPanel.add(textField);
		textField.setColumns(10);
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
						try{
							Configuration config=new Configuration();
							String rmiAddress=config.getRmiAddress();
							SaleComInterface SCI=(SaleComInterface) Naming.lookup(rmiAddress+"SaleCom");
							
							double mr=Double.parseDouble(textField.getText());
						if(SCI.marketRes(EnterSaleCom_GUI.ComID, mr))
							{
							JOptionPane.showMessageDialog(null, "成功进行市场调查！！");
							WorkEffect_GUI dialog;
							try {
								dialog = new WorkEffect_GUI();
								dialog.setLocationRelativeTo(null);
								dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
								dialog.setVisible(true);
							} catch (MalformedURLException e1) {
							e1.printStackTrace();
							} catch (RemoteException e1) {
								e1.printStackTrace();
							} catch (NotBoundException e1) {
								e1.printStackTrace();
							}
							}
							else
							{
								JOptionPane.showMessageDialog(null, "公司资金不足或者投资金额超出预算！！");
							}
						}
						catch(Exception e1)
						{
							e1.printStackTrace();
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
						SaleComWorker_3_GUI dialog = new SaleComWorker_3_GUI();
						dialog.setLocationRelativeTo(null);
						dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						dialog.setVisible(true);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		
	}
}
